package com.phoenix.filemanager;
import java.util.ArrayList;

import com.phoenix.Ulties.FileManagerUlties;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileManageBaseAdapter extends BaseAdapter {
	private Context context;
	private final ArrayList<String> list_file;
	private String path_file;
 
	public FileManageBaseAdapter(Context context,String path, ArrayList<String> list_file) {
		this.context = context;
		this.list_file=list_file;
		this.path_file=path;
	}
 
	public View getView(int position, View convertView, ViewGroup parent) {
 
		LayoutInflater mInflater = (LayoutInflater) context
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		ViewHolder holder;  

		if (convertView == null) 
		{  

		convertView = mInflater.inflate(R.layout.gridview_mobile, null);  
		holder = new ViewHolder();  
		
		holder.label_txt = (TextView) convertView.findViewById(R.id.grid_item_label);  
		holder.img_txt = (ImageView) convertView.findViewById(R.id.grid_item_image);  
		
		convertView.setTag(holder);  
		}
		else
		{
			  holder = (ViewHolder) convertView.getTag();  
		}
//		check directory
		String path = path_file + "/" + list_file.get(position);

		Boolean is_folder = FileManagerUlties.isDirectory(path);
		if (is_folder==true) {
			holder.img_txt.setImageResource(R.drawable.folder);			
		}
		else
		{
			
			if (FileManagerUlties.isImageFile(list_file.get(position))==true) {
				
				holder.img_txt.setImageResource(R.drawable.image);				
			}
			else
			holder.img_txt.setImageResource(R.drawable.file);					
		}
		
		holder.label_txt.setText(list_file.get(position));

		return convertView;
	}
	static class ViewHolder 
	{  
		TextView label_txt;  
		ImageView img_txt;  
	}
 
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list_file.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
 
}